package com.ha.garbage;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ha.garbage.adapter.NewsListAdapter;
import com.ha.garbage.adapter.OrderListAdapter;
import com.ha.garbage.context.BaseContext;
import com.ha.garbage.model.OrderVO;
import com.ha.garbage.model.Result;
import com.ha.garbage.service.OrderService;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class MineFragment extends Fragment {

private RecyclerView recyclerView;
private List<OrderVO> orderVOList = new ArrayList<>();

private Button btn_change;
private Button btn_card;
private TextView tv_user_name;
private Button btn_order;
private TextView tv_user_credit;
private OrderListAdapter orderListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 初始化控件
        recyclerView = view.findViewById(R.id.rv_order);
        btn_card = view.findViewById(R.id.btn_card);
        btn_change = view.findViewById(R.id.btn_change);
        tv_user_credit= view.findViewById(R.id.tv_user_credit);
        tv_user_credit.setText(BaseContext.getCurrentCreditPoint() != null ? BaseContext.getCurrentCreditPoint().toString() :"登录后查看");

        tv_user_credit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = UserRetrofit.retrofit();
                userService userService = retrofit.create(userService.class);
                if(BaseContext.getCurrentId()!=null){
                    Call<Result<Integer>> point = userService.getPoint(BaseContext.getCurrentId(),BaseContext.getCurrentToken());
                    point.enqueue(new Callback<Result<Integer>>() {
                        @Override
                        public void onResponse(Call<Result<Integer>> call, Response<Result<Integer>> response) {
                            if(response.isSuccessful()){
                                Result<Integer> body = response.body();
                                if(body.getCode() == 1){
                                    tv_user_credit.setText(body.getData().toString());
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<Result<Integer>> call, Throwable t) {

                            System.out.println("请求路径为"+call.request().url().toString());
                            System.out.println("错误原因"+t.toString());
                        }
                    });
                }

            }
        });

        tv_user_name = view.findViewById(R.id.tv_user_name);

        tv_user_name.setText(BaseContext.getCurrentName() != null ? BaseContext.getCurrentName() : "游客登录");

        btn_order = view.findViewById(R.id.btn_order);



        Retrofit retrofit = UserRetrofit.retrofit();
        OrderService orderService = retrofit.create(OrderService.class);
        Call<Result<List<OrderVO>>> byUserId = orderService.getByUserId(BaseContext.getCurrentId());
        byUserId.enqueue(new Callback<Result<List<OrderVO>>>() {
            @Override
            public void onResponse(Call<Result<List<OrderVO>>> call, Response<Result<List<OrderVO>>> response) {
                if(response.isSuccessful()){
                    Result<List<OrderVO>> body = response.body();
                    if(body.getCode() == 1){
                        orderVOList = body.getData();
                        //初始化适配器
                        orderListAdapter = new OrderListAdapter(orderVOList);
                        //设置适配器
                        recyclerView.setAdapter(orderListAdapter);
                    }else {
                        new AlertDialog.Builder(getContext())
                                .setMessage("没有订单信息")
                                .setPositiveButton("确定",null)
                                .show();
                    }
                }
            }

            @Override
            public void onFailure(Call<Result<List<OrderVO>>> call, Throwable t) {
                System.out.println("请求路径为："+call.request().url().toString());
                System.out.println("资讯服务错误");
                t.printStackTrace();
            }
        });

        btn_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Call<Result<List<OrderVO>>> byUserId1 = orderService.getByUserId(BaseContext.getCurrentId());
                byUserId1.enqueue(new Callback<Result<List<OrderVO>>>() {
                    @Override
                    public void onResponse(Call<Result<List<OrderVO>>> call, Response<Result<List<OrderVO>>> response) {
                        if(response.isSuccessful()){
                            Result<List<OrderVO>> body = response.body();
                            if(body.getCode() == 1){
                                orderVOList = body.getData();
                                //初始化适配器
                                orderListAdapter = new OrderListAdapter(orderVOList);
                                //设置适配器
                                recyclerView.setAdapter(orderListAdapter);
                            }else {
                                new AlertDialog.Builder(getContext())
                                        .setMessage("没有订单信息")
                                        .setPositiveButton("确定",null)
                                        .show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<List<OrderVO>>> call, Throwable t) {
                        System.out.println("请求路径为："+call.request().url().toString());
                        System.out.println("资讯服务错误");
                        t.printStackTrace();
                    }
                });
            }
        });

        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(BaseContext.getCurrentId()==null){
                    new AlertDialog.Builder(getContext())
                            .setMessage("请先登录")
                            .setPositiveButton("知道了",null)
                            .show();
                    return;
                }
                Intent intent = new Intent(getContext(),ModifyActivity.class);
                startActivity(intent);


            }
        });
        btn_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(BaseContext.getCurrentId() == null){
                    new AlertDialog.Builder(getContext())
                            .setMessage("请先登录")
                            .setPositiveButton("知道了",null)
                            .show();
                }else {
                    //调用方法签到加分

                    userService userService = retrofit.create(userService.class);
                    Call<Result> resultCall = userService.setPoint(1, BaseContext.getCurrentId(),BaseContext.getCurrentToken());
                    resultCall.enqueue(new Callback<Result>() {
                        @Override
                        public void onResponse(Call<Result> call, Response<Result> response) {
                            if(response.isSuccessful()){
                                Result result = response.body();
                                if(result.getCode() == 1){
                                    new AlertDialog.Builder(getContext())
                                            .setMessage("签到成功")
                                            .setPositiveButton("确定",null)
                                            .show();
                                    BaseContext.setCurrentCreditPoint(BaseContext.getCurrentCreditPoint()+20);
                                    tv_user_credit.setText(BaseContext.getCurrentCreditPoint().toString());
                                }else {
                                    new AlertDialog.Builder(getContext())
                                            .setMessage("今日已签到")
                                            .setPositiveButton("确定",null)
                                            .show();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<Result> call, Throwable t) {

                            System.out.println("请求路径"+call.request().url().toString());
                            System.out.println("错误原因"+t.toString());
                        }
                    });
                }

            }
        });

    }
}